import { BrowserWindow, Menu, app, Tray } from 'electron'
import { platform } from "os"
import path from 'path';
import menuconfig from '../config/menu'
import config from '@config'
import setIpc from './ipcMain'
import { winURL, loadingURL } from '../config/StaticPath'

var loadWindow = null
var mainWindow = null
let tray = null
setIpc.Mainfunc(config.IsUseSysTitle)

function createMainWindow() {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 800,
    useContentSize: true,
    width: 1700,
    minWidth: 1366,
    show: false,
    frame: config.IsUseSysTitle,
    titleBarStyle: platform().includes('win32') ? 'default' : 'hidden',
    webPreferences: {
      contextIsolation: false,
      nodeIntegration: true,
      webSecurity: false,
      // 如果是开发模式可以使用devTools
      devTools: process.env.NODE_ENV === 'development',
      // devTools: true,
      // 在macos中启用橡皮动画
      scrollBounce: process.platform === 'darwin'
    }
  })
  // 这里设置只有开发环境才注入显示开发者模式
  if (process.env.NODE_ENV === 'development') {
    menuconfig.push({
      label: '开发者设置',
      submenu: [{
        label: '切换到开发者模式',
        accelerator: 'CmdOrCtrl+I',
        role: 'toggledevtools'
      }]
    })
  }
  // 载入菜单
  const menu = Menu.buildFromTemplate(menuconfig)
  Menu.setApplicationMenu(menu)
  mainWindow.loadURL(winURL)


  mainWindow.webContents.once('dom-ready', () => {
    mainWindow.show()
    if (process.env.NODE_ENV === 'development') mainWindow.webContents.openDevTools(true)
    if (config.UseStartupChart) loadWindow.destroy()
  })
  mainWindow.on('maximize',()=>{
      mainWindow.webContents.send("w-max",true)
  })
  mainWindow.on('unmaximize',()=>{
      mainWindow.webContents.send("w-max",false)
  })
  mainWindow.on('closed', () => {
    mainWindow = null
    app.quit();
  })
  // 触发关闭时触发
  mainWindow.on('close', (event) => {
    // 截获 close 默认行为
    event.preventDefault();
    // 点击关闭时触发close事件，我们按照之前的思路在关闭时，隐藏窗口，隐藏任务栏窗口
    mainWindow.hide();
    mainWindow.setSkipTaskbar(true);

  });

  app.whenReady().then(() => {
      tray = new Tray(`${__static}/icons/icon.ico`)
      const contextMenu = Menu.buildFromTemplate([
          {
              label: '显示',
              icon: `${__static}/icons/open.png`,
              click: () => { mainWindow.show() }
          },
          {
              label: '退出',
              icon: `${__static}/icons/quit.png`,
              click: function () {
                  app.quit()
                  tray.destroy()
                  mainWindow.close()
              }
          }
      ])
      tray.setToolTip('PC助手')
      tray.setContextMenu(contextMenu)

      // 单击托盘小图标显示应用
      tray.on('click', () => {
          // 显示主程序
          mainWindow.show();
          // 关闭托盘显示
          //appTray.destroy();
      });
  })

}

function loadingWindow() {
  loadWindow = new BrowserWindow({
    width: 400,
    height: 600,
    frame: false,
    backgroundColor: '#222',
    skipTaskbar: true,
    transparent: true,
    resizable: false,
    webPreferences: { experimentalFeatures: true }
  })

  loadWindow.loadURL(loadingURL)

  loadWindow.show()

  setTimeout(() => {
    createMainWindow()
  }, 2000)

  loadWindow.on('closed', () => {
    loadWindow = null
  })
}

function initWindow() {
  if (config.UseStartupChart) {
    return loadingWindow()
  } else {
    return createMainWindow()
  }
}
export default initWindow
